import * as modal from '@/utils/modal'
// 生成随机数 --- 永不重复
export function createRandomId () {
  return (Math.random() * 10).toString().substr(2, 5) + '-' + Math.random().toString().substr(2, 5) + '-' + (new Date()).getTime()
}

// 获取时间
export function timeStamp () {
  const date = new Date()

  let year = date.getFullYear()
  let month = date.getMonth() + 1
  let day = date.getDate()
  let hour = date.getHours()
  let minute = date.getMinutes()
  let second = date.getSeconds()

  return year + '-' + (month >= 10 ? month : '0' + month) + '-' + (day >= 10 ? day : '0' + day) + ' ' + (hour >= 10 ? hour : '0' + hour) + ':' + (minute >= 10 ? minute : '0' + minute) + ':' + (second >= 10 ? second : '0' + second)
}

// 深拷贝
export function deepClone (value) {
  let valueType = Array.isArray(value) ? [] : {}
  if (value && typeof value === 'object') {
    for (let key in value) {
      if (value.hasOwnProperty(key)) {
        if (value[key] && typeof value[key] === 'object') {
          // 递归
          valueType[key] = deepClone(value[key])
        } else {
          // 如果不是，简单复制
          valueType[key] = value[key]
        }
      }
    }
  }
  return valueType
}

// 路由配置函数
export const getRouter = (path, name) => () => import('@/' + path + '/' + name)

/**
 * 初始化搜索框数据
 * @param {Array|Object} arr 传入的数组
 * @param {String} value 用户选择后对应的值
 * @param {String} label 显示的label
 */
export function initSelectValue (arr, value, label) {
  let newArr = []
  if (Object.prototype.toString.call(arr) === '[object Array]' && arr.length !== 0) {
    value && label
      ? arr.map(item => newArr.push({ value: item[value], label: item[label] }))
      : arr.map(item => newArr.push({ value: item, label: item }))
    return newArr
  }
  modal.warning(this, '数据错误请联系开发人员')
  return false
}

/**
 * 多选数组转字符串、键值对数组、源数组
 * @param {Array} list 传入的数组
 * @param {Boolean} isString 是否转换为字符串
 * @param {String} sign 转换为字符串时连接符
 * @param {Boolean} isKvalue 是否转换为键值对数组
 * @param {String} keyName 键名
 */
export function arrangeDataList (list, isString, sign = ',', isKvalue, keyName) {
  let arr = []
  if (isString) {
    return list.join(`${sign}`)
  }

  isKvalue
    ? list.map(item => arr.push({ id: item[keyName] }))
    : list.map(item => arr.push(item[keyName]))
  return arr
}
